/** 
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

import java.io.Serializable;
import java.util.Hashtable;

/**
 * All possible command tuple's types. This class is designed as enum which can
 * be used since JDK 1.5. But for JDK 1.3 compatible reason, we do not use enum
 * facility directly.
 * 
 * @author Xing Jiankuan
 * 
 */
@SuppressWarnings("serial")
public class CommandType implements Serializable {
	public final static CommandType ADD_REPLICA_REQUEST = new CommandType(
			"addReplicaRequest");
	public final static CommandType REMOVE_REPLICA_REQUEST = new CommandType(
			"removeReplicaRequest");
	public final static CommandType UPDATE_REPLICA = new CommandType(
			"updateReplica");
	public final static CommandType CLEAR_REPLICA = new CommandType(
			"clearReplica");
	public final static CommandType PUSH = new CommandType("push");
	private String typeName;

	public String getTypeName() {
		return typeName;
	}

	private CommandType(String type) {
		this.typeName = type;
	}

	// Map<type name, CommandType>
	static private Hashtable types = new Hashtable();
	static {
		types.put(ADD_REPLICA_REQUEST.getTypeName(), ADD_REPLICA_REQUEST);
		types.put(REMOVE_REPLICA_REQUEST.getTypeName(), REMOVE_REPLICA_REQUEST);
		types.put(UPDATE_REPLICA.getTypeName(), UPDATE_REPLICA);
		types.put(CLEAR_REPLICA.getTypeName(), CLEAR_REPLICA);
		types.put(PUSH.getTypeName(), PUSH);
	}

	static CommandType get(String typeName) {
		return (CommandType) types.get(typeName);
	}

	public String toString() {
		return typeName;
	}

	public boolean equals(Object o) {
		if (super.equals(o))
			return true;
		if (!(o instanceof CommandType))
			return false;
		CommandType ct = (CommandType) o;
		if (this.typeName.equals(ct.typeName))
			return true;
		else
			return false;
	}
}
